<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入证书名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="tabActiveName"  @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane label="待审核" name="1"></el-tab-pane>
      <el-tab-pane label="已通过" name="2"></el-tab-pane>
      <el-tab-pane label="未通过" name="3"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="certList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="证书名称" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="fileUrl" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.fileUrl"
            style="width: 50px; height: auto"
            :src="scope.row.fileUrl" 
            :preview-src-list="[scope.row.fileUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="证书范围" align="center" prop="tags" />
      <el-table-column label="审核状态" align="center" prop="status" >
            <template slot-scope="scope">
              <span v-if="scope.row.status == 0">待提交</span>
              <span v-if="scope.row.status == 1">待审核</span>
              <span v-if="scope.row.status == 2">已通过</span>
              <span v-if="scope.row.status == 3">未通过</span>
            </template>
          </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text" v-if="scope.row.status == 1"
            icon="el-icon-coordinate"
            @click="handleApprove(scope.row)"
          >审核</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户-证书记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" disabled>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="form.userName" placeholder="用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="手机号" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="证书名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入证书名称" />
        </el-form-item>
        <el-form-item label="证书图片">          
          <imageUpload v-model="form.fileUrl"/>
        </el-form-item>
        <el-form-item label="证书范围" prop="tags">
          <el-input v-model="form.tags" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitPass" v-if="approved">通 过</el-button>
        <el-button type="warning" @click="submitNotPass" v-if="approved">驳 回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCert, getCert, delCert, addCert, updateCert, exportCert } from "@/api/business/cert";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Cert",
  components: {
    ImageUpload,
  },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      approved: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户-证书记录表格数据
      certList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        fileUrl: null,
        tags: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户-证书记录列表 */
    getList() {
      this.loading = true;
      // this.queryParams.status = 1;
      listCert(this.queryParams).then(response => {
        this.certList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.approved = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        fileUrl: null,
        tags: null,
        createTime: null,
        status: "0"
      };
      this.resetForm("form");
    },
    handleTabClick(tab){
      if (tab.name != 'all') {
        this.queryParams.status = tab.name;
      }else{
        this.queryParams.status = '';
      }
      this.getList();

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户-证书记录";
    },
    /** 审核按钮操作 */
    handleApprove(row) {
      this.reset();
      this.approved = true;
      const id = row.id || this.ids
      getCert(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "证书记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCert(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCert(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitPass: function() {
      this.form.status = 2;
      updateCert({
        id: this.form.id,
        status: this.form.status
      }).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
     /** 提交按钮 */
    submitNotPass: function(){
      this.form.status = 3;
      updateCert({
        id: this.form.id,
        status: this.form.status
      }).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
  }
};
</script>
